package com.leetcode.binarySearch;

public class SemiOrderedPermutation {
    public static void main(String[] args) {

    }

    public static int countSwap(int[] nums) {
        int max = nums.length;
        int min = 1;
        int minValInd = 0;
        int maxValInd = 0;
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == min) {
                minValInd = i;
            } else if (nums[i] == max) {
                maxValInd = i;
            } else if (minValInd != 0 && maxValInd != 0) {
                break;
            }
        }
        if (minValInd < maxValInd) {
            res += minValInd;
            res += nums.length - 1 - maxValInd;
        } else if (minValInd > maxValInd){
            res += minValInd;
            res += nums.length - 1 - maxValInd - 1;
        } // 不存在最小值索引与最大值索引相同的情况
        return res;
    }
}
